package com.dragon.manage.action;

import com.alibaba.fastjson.JSONObject;
import com.dragon.manage.bean.ManageUser;
import com.dragon.manage.service.IManageNewsService;
import com.dragon.manage.service.IManageService;
import com.dragon.manage.util.LogUtil;
import com.dragon.manage.util.ManageUtil;
import com.dragon.manage.util.RightUtil;
import com.dragon.manage.util.StaticData;
import com.dragon.manage.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;

/**
 * 资讯管理
 * @author 34225_000
 *
 */
@SuppressWarnings("rawtypes")
public class ManageNewsAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private IManageService manageService;
	
	private IManageNewsService manageNewsService;
	
	public void setManageService(IManageService manageService) {
		this.manageService = manageService;
	}
	
	
	public void setManageNewsService(IManageNewsService manageNewsService) {
		this.manageNewsService = manageNewsService;
	}

	public String deleteNews(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String newid=request.getParameter("newId");
			if(newid==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			result = manageNewsService.deleteNews(StringUtil.getParam(newid));
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("修改密码异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	/**
	 * 查询新闻列表
	 * @return
	 */
	public String queryNewsList(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String page=request.getParameter("page");
			
			result = manageNewsService.queryNewsList(StringUtil.getParam(page));
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("修改密码异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
		
	}
	
	/**
	 * 保存新闻
	 * @return
	 */
	public String saveNews(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String title=request.getParameter("title");
			String content=request.getParameter("content");
			String coveraddress=request.getParameter("coveraddress");
			String newstype=request.getParameter("newstype");
			String h5url=request.getParameter("h5url");
			if(title==null||content==null||newstype==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			
			result = manageNewsService.saveNews(title, content, coveraddress, 
					StringUtil.getParam(newstype), h5url);
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("修改密码异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 获取新闻信息
	 * @return
	 */
	public String getNewsInfo(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			
			String newid=request.getParameter("newId");
			if(newid==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			result = manageNewsService.getNewsInfo(StringUtil.getParam(newid));
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("修改密码异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 更新新闻信息
	 * @return
	 */
	public String updateNews(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			
			String newid=request.getParameter("newId");
			if(newid==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String title=request.getParameter("title");
			String content=request.getParameter("content");
			String coveraddress=request.getParameter("coveraddress");
			String newstype=request.getParameter("newstype");
			String h5url=request.getParameter("h5url");
			
			result = manageNewsService.updateNews(StringUtil.getParam(newid), title, content, 
					coveraddress, StringUtil.getParam(newstype), h5url);
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("修改密码异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 保存新闻类型
	 * @return
	 */
	public String saveNewsType(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String typename=request.getParameter("typename");
			String typedesc=request.getParameter("typedesc");
			String logoaddress=request.getParameter("logoaddress");
			String parentid=request.getParameter("parentId");
			if(typename==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			result = manageNewsService.saveNewsType(typename, typedesc, 
					logoaddress, StringUtil.getParam(parentid));
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("修改密码异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 更新新闻类型
	 * @return
	 */
	public String updateNewsType(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String typeid=request.getParameter("typeid");
			if(typeid==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String typename=request.getParameter("typename");
			String typedesc=request.getParameter("typedesc");
			String logoaddress=request.getParameter("logoaddress");
			String parentid=request.getParameter("parentid");
			result = manageNewsService.updateNewsType( StringUtil.getParam(typeid), typename, typedesc, 
					logoaddress,  StringUtil.getParam(parentid));
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("修改密码异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 删除新闻类型
	 * @return
	 */
	public String deleteNewsType(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String typeid=request.getParameter("typeId");
			if(typeid==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			result = manageNewsService.deleteNewsType(StringUtil.getParam(typeid));
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("修改密码异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 查询新闻类型
	 * @return
	 */
	public String queryNewsTypeList(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String typeid=request.getParameter("typeId");
			result = manageNewsService.queryNewsTypeList(StringUtil.getParam(typeid));
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("修改密码异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 保存新闻关系
	 * @return
	 */
	public String saveNewsDiscussRela(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("修改密码异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 查询导航条
	 * @return
	 */
	public String queryBannerList(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String page=request.getParameter("page");
			
			String bannerId=request.getParameter("bannerId");
			result = manageNewsService.queryBannerList(StringUtil.getParam(page),
					StringUtil.getParam(bannerId));
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("修改密码异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 新建导航条
	 * @return
	 */
	public String saveBanner(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String bannername=request.getParameter("bannername");
			String coveraddress=request.getParameter("coveraddress");
			String h5url=request.getParameter("h5url");
			String newid=request.getParameter("newId");
			String matchid=request.getParameter("matchId");
			String position=request.getParameter("position");
			int bannertype=StaticData.no_param;
			if(bannername==null||coveraddress==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			/*
			 * 1-资讯，2-外链，3-比赛
			 */
			if(newid!=null){
				bannertype = StaticData.bannerType.news.getType();
			}else if(h5url!=null){
				bannertype = StaticData.bannerType.h5Url.getType();
			}else if(matchid!=null){
				bannertype = StaticData.bannerType.match.getType();
			}
			
			result = manageNewsService.saveBanner(bannername, coveraddress, 
					h5url, StringUtil.getParam(newid),StringUtil.getParam(matchid),
					StringUtil.getParam(position),bannertype);
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("修改密码异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 更新导航条
	 * @return
	 */
	public String updateBanner(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String bannerId=request.getParameter("bannerId");
			if(bannerId==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String bannername=request.getParameter("bannername");
			String coveraddress=request.getParameter("coveraddress");
			String h5url=request.getParameter("h5url");
			String newid=request.getParameter("newId");
			String matchid=request.getParameter("matchId");
			String position=request.getParameter("position");
			String bannertype=request.getParameter("bannertype");
			result = manageNewsService.updateBanner(StringUtil.getParam(bannerId), bannername, coveraddress,
					h5url, StringUtil.getParam(newid),StringUtil.getParam(matchid),
					StringUtil.getParam(position),StringUtil.getParam(bannertype));
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("更新导航条", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 删除导航条
	 * @return
	 */
	public String deleteBanner(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String bannerId=request.getParameter("bannerId");
			if(bannerId==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			
			result = manageNewsService.deleteBanner(StringUtil.getParam(bannerId));
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("删除导航条", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
}
